import numpy as np


def signal(*args):
    df = args[0]
    n = args[1]
    factor_name = args[2]

    """
        https://bbs.quantclass.cn/thread/8329
         3均线趋势过滤器
          param:
            n: 短周期
            m, 中周期
            k, 长周期
    """

    m = n * 2
    k = m * 2

    df['small_ma'] = df['close'].rolling(window=n).mean()
    df['media_ma'] = df['close'].rolling(window=m).mean()
    df['big_ma'] = df['close'].rolling(window=k).mean()
    # ===计算信号
    df[factor_name] = 0
    df.loc[(df['small_ma'] > df['media_ma']) & (df['media_ma'] > df['big_ma']), factor_name] = 1
    df.loc[(df['big_ma'] > df['media_ma']) & (df['media_ma'] > df['small_ma']), factor_name] = -1

    del df["small_ma"]
    del df["media_ma"]
    del df["big_ma"]

    return df
